import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class PackApk {

    public static final boolean IS_WINDOWS = System.getProperty("os.name")
            .toString().toLowerCase().startsWith("window");
    public static final String PATH_SEPARATE = IS_WINDOWS ? "\\" : "/";

    private static String SOURCE_PATH = ".";
    private static String RESOURCE_PATH;

    private static final String CONFIG_NAME = "config_android.txt";

    public static void main(String[] args) {
        String path = null;
        String fileName = null;

        if (args.length > 1) {
            path = args[0];
            fileName = args[1];
            GenerateQRCode.readApkLog(path, fileName);
            return;
        }

        System.out.println("请输入源代码路径(默认当前文件夹):");
        BufferedReader strin = new BufferedReader(new InputStreamReader(
                System.in));
        try {
            SOURCE_PATH = strin.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        if (SOURCE_PATH == null || SOURCE_PATH.trim().equals("")) {
            SOURCE_PATH = ".";
        }

        System.out.println("请输入配资源文件所在路径:");
        try {
            RESOURCE_PATH = strin.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("正在检测图片和配置文件，请稍等................");
        System.out.println("*******************************************");
        System.out.println("*******************************************");
        if (!CheckConfig.check(RESOURCE_PATH)) {
            System.out.println("资源文件有问题，请检查!");
            System.exit(1);
            return;
        }

        System.out.println("正在拷贝资源和配置文件,请稍等..................");
        System.out.println("*******************************************");
        System.out.println("*******************************************");
        try {
            copyAllRes();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

    private static void copyAllRes() throws IOException {
        String[] flavors = new File(RESOURCE_PATH).list();
        flavors = Utils.removeHiddleDir(flavors);

        for (String flavor : flavors) {
            String path = RESOURCE_PATH + PATH_SEPARATE + flavor
                    + PATH_SEPARATE;
            CopyResource.copyFile(path + CONFIG_NAME, SOURCE_PATH
                    + PATH_SEPARATE + "vendor" + PATH_SEPARATE + "config"
                    + PATH_SEPARATE + "config_" + flavor + ".properties");

            CopyResource.doCopyRes(path + "pic" + PATH_SEPARATE, SOURCE_PATH
                    + PATH_SEPARATE + "vendor" + PATH_SEPARATE + "fixed"
                    + PATH_SEPARATE + flavor + PATH_SEPARATE + "res"
                    + PATH_SEPARATE);

            CopyResource.copyKeyFile(path, SOURCE_PATH + PATH_SEPARATE + "app"
                    + PATH_SEPARATE);

            CopyResource.copyUserAgreementFile(path, SOURCE_PATH + "/app/src/main/assets");

            CopyResource.copyAGCPFile(path, SOURCE_PATH
                    + PATH_SEPARATE + "vendor" + PATH_SEPARATE + "fixed"
                    + PATH_SEPARATE + flavor);
        }
        writeFlavor(flavors);
    }

    private static void writeFlavor(String[] flavors) {
        String flavorWindowsStr = "call gradlew.bat clean && call gradlew.bat ";
        String flavorLinuxStr = "chmod +x ./gradlew && ./gradlew clean && ./gradlew ";

        for (String flavor : flavors) {
            flavorWindowsStr += " assemble" + flavor + "Release";
            flavorLinuxStr += " assemble" + flavor + "Release";
        }

        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(new File("flavors.bat"));
            fileWriter.write(flavorWindowsStr);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStreamWriter out;
        try {
            out = new OutputStreamWriter(new FileOutputStream("flavors.sh"),
                    "UTF-8");
            out.write(flavorLinuxStr);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
